
package com.yili.oa.service.impl;

import com.yili.oa.dao.RoleMapper;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.models.Role;
import com.yili.oa.models.RoleExample;
import com.yili.oa.service.RoleService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.vo.request.RoleDataReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;


/**
 * @author lxr
 * @version v0.01
 * @date 2019/3/28 0028
 * @email 200890949@qq.com
 */

@SuppressWarnings("ALL")
@Service
@Transactional
public class RoleSvImpl implements RoleService {

    @Autowired
    private RoleMapper mapper;

    @Override
    public Boolean add(RoleDataReq roleDataReq, List<String> permissionIds) {
        Role role = new Role();
        role.setRoleId(UUID.randomUUID().toString());
        role.setRoleName(roleDataReq.getRoleName());
        role.setRoleDecription(roleDataReq.getRoleDecription());
        role.setRoleKey(roleDataReq.getRoleKey());
        StringBuffer sb = new StringBuffer();
        for (String permissionId:
             permissionIds) {
            if (StringUtils.isBlank(sb)){
                sb.append(permissionId);
            }else {
                sb.append(","+permissionId);
            }
        }
        role.setRolePermission(sb.toString());
        try {
            mapper.insert(role);
        }catch (Exception e){
            throw new AjaxException("出现错误 请重试");
        }
        return true;
    }

    @Override
    public Boolean update(RoleDataReq roleDataReq, List<String> permissionIds) {
        Role role = mapper.selectByPrimaryKey(roleDataReq.getRoleId());
        if (role==null){
            throw new AjaxException("该角色不存在");
        }
        role.setRoleName(roleDataReq.getRoleName());
        role.setRoleDecription(roleDataReq.getRoleDecription());
        role.setRoleKey(roleDataReq.getRoleKey());
        StringBuffer sb = new StringBuffer();
        for (String permissionId:
                permissionIds) {
            if (StringUtils.isBlank(sb)){
                sb.append(permissionId);
            }else {
                sb.append(","+permissionId);
            }
        }
        role.setRolePermission(sb.toString());
        try {
            mapper.updateByPrimaryKeySelective(role);
        }catch (Exception e){
            throw new AjaxException("出现错误 请重试");
        }
        return true;

    }

    @Override
    public List<Role> getList(SearchBase searchBase) {
        RoleExample example = new RoleExample();
        return mapper.selectByExample(example);
    }

    @Override
    public Role getById(String roleId) {
        return mapper.selectByPrimaryKey(roleId);
    }

    @Override
    public Boolean del(List<String> roleIds) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdIn(roleIds);
        try {
            mapper.deleteByExample(example);
        }catch (Exception e){
            throw new AjaxException("出现错误 请重试");
        }
        return true;
    }

    @Override
    public Boolean checkName(String name, String id) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleNameEqualTo(name);
        if (id!=null){
            criteria.andRoleIdNotEqualTo(id);
        }
        List<Role> list = mapper.selectByExample(example);
        if(ListUtils.isNotNullOrEmpty(list)){
            return false;
        }
        return true;
    }

    @Override
    public Boolean updPermission(String id,List<String> permissionIds) {
        Role role = mapper.selectByPrimaryKey(id);
        if (role==null){
            throw new AjaxException("该角色不存在");
        }
        StringBuffer sb = new StringBuffer();
        for (String permissionId:
                permissionIds) {
            if (StringUtils.isBlank(sb)){
                sb.append(permissionId);
            }else {
                sb.append(","+permissionId);
            }
        }
        role.setRolePermission(sb.toString());
        try {
            mapper.updateByPrimaryKeySelective(role);
        }catch (Exception e){
            throw new AjaxException("出现错误 请重试");
        }
        return true;
    }

    @Override
    public Boolean checkCode(String code, String id) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleKeyEqualTo(code);
        if (id!=null){
            criteria.andRoleIdNotEqualTo(id);
        }
        List<Role> list = mapper.selectByExample(example);
        if(ListUtils.isNotNullOrEmpty(list)){
            return false;
        }
        return true;
    }
}

